package com.chuanghai.light.service;

import com.chuanghai.light.dao.LightPointDao;
import com.chuanghai.light.entity.LightPoint;
import com.chuanghai.light.entity.LightTimeList;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LightPointService  {

	@Resource
	private LightPointDao lightPointDao;

	public boolean addLightPoint(String pointCode,String pointName,String ip){
		LightPoint lightPointCode = lightPointDao.findByPointCode(pointCode);
		LightPoint lightPointIp = lightPointDao.findByIp(ip);
		if(lightPointCode!=null && lightPointIp!=null){  // 已经存在
			return false;
		}else{
			LightPoint newLightPoint = new LightPoint();
			newLightPoint.setPointCode(pointCode);
			newLightPoint.setPointName(pointName);
			newLightPoint.setIp(ip);
			newLightPoint.setOnline(false);
			lightPointDao.save(newLightPoint);
		}
		return true;
	}

	/**
	 * 获取表中所有点位信息
	 * @return
	 */
	public List<LightPoint> queryAllLightPoint(){
		return lightPointDao.findAll();
	}

	/**
	 * 修改指定地址编号的状态
	 * @param code
	 * @param state
	 * @return
	 */
	public Boolean changeStateByCode(String code, Boolean state){
		LightPoint lightPoint = lightPointDao.findByPointCode(code);
		lightPoint.setState(state);
		lightPointDao.save(lightPoint);
		return state;
	}

	/**
	 * 获取指定点位灯的状态
	 * @param code
	 * @return
	 */
	public Boolean getStateByCode(String code){
		LightPoint lightPoint = lightPointDao.findByPointCode(code);
		return lightPoint.getState();
	}


	/**
	 * 根据IP查询编号
	 * @param ip
	 * @return
	 */
	public String getPointCodeByIp(String ip){
		LightPoint lightPoint = lightPointDao.findByIp(ip);
		return lightPoint.getPointCode();
	}

	/**
	 * 修改所有灯的状态
	 * @param state
	 * @return
	 */
	public Boolean changeAllState(Boolean state){
		List<LightPoint> allPoint = lightPointDao.findAll();
		for (LightPoint point:allPoint) {
			point.setState(state);
			lightPointDao.save(point);
		}
		return state;
	}

	/**
	 * 修改指定IP的定位在线状态
	 * @param ip
	 * @param online
	 * @return
	 */
	public Boolean changeOnlineByIp(String ip, Boolean online){
		LightPoint lightPoint = lightPointDao.findByIp(ip);
		lightPoint.setOnline(online);
		lightPointDao.save(lightPoint);
		return online;
	}

	/**
	 * 查询指定点位灯的实时状态
	 * @param pointCode
	 * @return
	 */
	public boolean findStatePointCode(String pointCode){
		return lightPointDao.findStateByPointCode(pointCode);
	}

	/**
	 * 获取指定点位灯的网络状态
	 * @param code
	 * @return
	 */
	public Boolean getOnlineByPointCode(String code){
		return lightPointDao.findOnlineByPointCode(code);
	}

	/**
	 * 设置指定点位灯的网络状态
	 * @param pointCode
	 * @param online
	 */
	public void setOnlineByPointCode(String pointCode, boolean online){
		LightPoint lightPoint = lightPointDao.findByPointCode(pointCode);
		lightPoint.setOnline(online);
		lightPointDao.save(lightPoint);
	}


	/**
	 * 获取所有点位的网络和灯状态
	 * @return
	 */
	public Map<String, Object> getAllState(){
		List<Map<String, Object>> params = new ArrayList<>();
		List<LightPoint> lightPoint = lightPointDao.findAll();
		if (lightPoint!=null){
			for (LightPoint light : lightPoint) {
				Map<String, Object> param = new HashMap<>();
				param.put("pointCode", light.getPointCode());
				param.put("online", light.getOnline());
				param.put("state", light.getState());
				params.add(param);
			}
		}
		Map<String, Object> returnParam = new HashMap<>();
		returnParam.put("pointInfo",params);
		return returnParam;
	}
}
